var util = require('../../../libs/util.js');
var app = getApp();
Page({
  data: {
    id: '',
    name: '',
    photo: "/images/upload-add.svg"
  },
  onShareAppMessage: function () {
    return {
      title: app.appName,
      path: '/pages/index/index'
    }
  },
  onLoad: function (e) {
    wx.setNavigationBarTitle({ "title": app.appName +"--新增货品" });
    this.setData({
      id: e.id,
      name: e.name,
      wholemile: e.wholemile,
      mile: e.mile,
      subsidy: e.subsidy
    });
  },
  onReady: function () {
    wx.hideToast()
  },
  bindInput: function (e) {
    var item = String(e.currentTarget.id), item_value = {};
    item_value[item] = e.detail.value;
    this.setData(item_value);
  },
  chooseImage: function (e) {
    var that = this;
    wx.chooseImage({
      sizeType: ['original', 'compressed'], // 可以指定是原图还是压缩图，默认二者都有
      sourceType: ['album', 'camera'], // 可以指定来源是相册还是相机，默认二者都有
      success: function (res) {
        // 返回选定照片的本地文件路径列表，tempFilePath可以作为img标签的src属性显示图片
        var item = String(e.currentTarget.id), item_value = {};
        item_value[item] = res.tempFilePaths[0];
        that.setData(item_value);
        wx.uploadFile({
          url: 'https://www.uamaker.com/wechat/wxapplet/uploadPicture',
          header:{
            'Authorization': app.globalData.token
          },
          filePath: res.tempFilePaths[0],
          name: 'file',
          formData: {
            'type': item
          },
          success: function (res) {console.log(res)
            var data = JSON.parse(res.data);
            if (data.success) {
              var upload_item = String(e.currentTarget.id), item_value = {};
              item_value[upload_item] = app.server + data.data[0].path;
              that.setData(item_value);
            }
          }
        })
      }
    })
  },
  previewImage: function (e) {
    wx.previewImage({
      current: e.currentTarget.id, // 当前显示图片的http链接
      urls: this.data.files // 需要预览的图片http链接列表
    })
  },
  confirmData: function () {
    var that = this;
    wx.showLoading({
      title: '请稍候...',
    });
    if (that.data.name == '' || that.data.name == undefined){
      wx.showToast({
        title: "请输入品名",
        image: '/images/wrong.png',
        duration: 3000
      });
      return false;
    }
    util.postData("goods", {
      name: that.data.name,
      remark: that.data.remark,
      photo: that.data.photo
    }, function (res, error) {
      //console.log(res); console.log(error)
      wx.hideLoading();
      if (error) {
        wx.showToast({
          title: res.data.data,
          image: '/images/wrong.png',
          duration: 3000
        });
        return false;
      }
      if (res.data.success) {
        wx.showLoading({
          title: "提交成功",
          icon: 'success',
          success: function () {
            setTimeout(function () {
              wx.redirectTo({
                url: '/pages/warehouse/goods'
              })
            }, 3000);
          }
        });
      }else{
        wx.showToast({
          title: res.data.data,
          image: '/images/wrong.png',
          duration: 3000
        });
      }
    });
  },
  cancelConfirmData: function () {
    wx.redirectTo({
      url: '/pages/warehouse/goods'
    })
  }
});